package com.tj.tj_java.mapper;

import com.tj.tj_java.pojo.Verification;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.sql.Timestamp;

@Mapper
public interface VerificationMapper {

    //根据邮箱号和当前时间判断当前邮箱验证码是否过期 过期时间>当前时间
    @Select("select * from verification where email = #{email} and expirationTime > #{timestamp} and codeType = #{codeType}" )
    Verification getVerificationByEmailAndTimeAndCodeType(String email,Timestamp timestamp,String codeType);
    //根据邮箱号和验证码查询邮箱
    @Select("select * from verification where email = #{email} and code = #{code} and codeType = #{codeType}")
    Verification getVerificationByEmailAndCode(String email, Integer code,String codeType);

    //保存 邮箱号和验证码 以及过期时间和邮箱类型
    @Insert("insert into verification(email,code,expirationTime,codeType) values (#{email},#{code},#{expirationTime},#{codeType})")
    void saveCode(String email, int code, Timestamp expirationTime,String codeType);

    //删除全部过期邮箱
    @Delete("delete from verification where expirationTime < #{timestamp}")
    void deleteTimeoutCode(Timestamp timestamp);

     //根据 邮箱号 和邮箱类型 删除验证码
    @Delete("delete from verification WHERE email = #{email} and codeType = #{codeType}")
    void deleteCode(String email,String codeType);

    /*------------------------*/
    //根据 邮箱号 和 验证码类型 查询 验证码记录
    @Select("select * from verification where email = #{email} and codeType = #{codeType}")
    Verification getVerificationByEmailAndCodeType(String email, String codeType);

}
